﻿#region
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using EndlessDownloader;
using EndlessDownloader.Engine;
using EndlessDownloaderEx.Properties;

#endregion

namespace EndlessDownloaderEx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            prbPage.Maximum = MaxPage;
        }

        private int _downloadIndex;

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
        }

        private void uscDownloadViewer_ButtonClicked(object sender, DownloadViewer.ButtonClickedEventArgs e)
        {
            ptbImage.Image = new Bitmap(e.SavedPath);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            bgwPage.RunWorkerAsync();
        }

        private readonly ConcurrentBag<Picture> _pictures = new ConcurrentBag<Picture>();

        private const int MaxPage = 2;

        private int pageCount;

        private void bgwPage_DoWork(object sender, DoWorkEventArgs e)
        {
            Parallel.For(1, MaxPage + 1, page =>
            {
                WebClient webClient = new WebClient();

                string url = "http://kv86.tumblr.com/page/" + page;

                string html = webClient.DownloadString(url);
                var imageUrls = ImageSourceParser.Instance.ExtractImageUrl(html, Settings.Default.NoPorn);

                Interlocked.Increment(ref pageCount);

                this.InvokeIfNeeded(() => prbPage.Value = pageCount);

                foreach (var imageUrl in imageUrls)
                {
                    Interlocked.Increment(ref _downloadIndex);

                    Picture picture = new Picture();
                    picture.DownloadIndex = _downloadIndex;
                    picture.Url = imageUrl;
                    picture.FileName = _downloadIndex.ToString("D" + Settings.Default.FileNameDigit) + Path.GetExtension(imageUrl);
                    picture.SavePath = Path.Combine(Settings.Default.SaveFolder, picture.FileName);

                    _pictures.Add(picture);

                    uscDownloadViewer.AddPicture(picture);
                }
            });
        }

        private void bgwPage_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            btnImage.Enabled = true;
        }

        private enum Progress
        {
            Start,
            Page,
            File
        }

        private void bgwImage_DoWork(object sender, DoWorkEventArgs e)
        {
            Parallel.ForEach(_pictures, picture =>
            {
                WebClient webClient = new WebClient();

                webClient.DownloadFile(picture.Url, picture.SavePath);
                uscDownloadViewer.SetAsCompleted(picture);

                if (Settings.Default.DownloadInterval > 0)
                    Thread.Sleep(Settings.Default.DownloadInterval * 1000);
            });
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            bgwImage.RunWorkerAsync();
        }
    }
}